﻿namespace CompareFiles
{
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Diagnostics;

    public class ComparedFile
    {
        /// <summary>
        /// Private constructor for ComparedFiles class
        /// </summary>
        /// <param name="inputfile">The full name of html file, which is a result of file comporation</param>
        private ComparedFile(string inputfile)
        {
            char CompareNumber = inputfile[inputfile.Length - 6];

            string path = Process.GetCurrentProcess().MainModule.FileName;
            string[] splitingpath = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            path = default(string);
            for (int i = 0; i < splitingpath.Length - 1; i++)
            {
                path = path + splitingpath[i] + '\\';
            }

            leftFile = path + "left" + CompareNumber + ".html";
            rightFile = path + "right" + CompareNumber + ".html";

            StreamWriter LeftStreamWriter = new StreamWriter(@leftFile);
            StreamWriter RightStreamWriter = new StreamWriter(@rightFile);

            StreamReader streamreader = new StreamReader(inputfile);

            int count = default(int);

            while (!streamreader.EndOfStream)
            {
                string StringToParse = streamreader.ReadLine();
                Regex openBrace = new Regex("<td[^>]*>");
                Match firstOpenBrace = openBrace.Match(StringToParse);

                if (firstOpenBrace.Success)
                {
                    if (count == 0)
                    {
                        count++;
                        LeftStreamWriter.WriteLine(StringToParse);
                        StringToParse = streamreader.ReadLine();
                        LeftStreamWriter.WriteLine(StringToParse);
                    }
                    else
                    {
                        StringToParse = streamreader.ReadLine();
                        RightStreamWriter.WriteLine(StringToParse);
                        StringToParse = streamreader.ReadLine();
                        RightStreamWriter.WriteLine(StringToParse);
                        count = 0;
                    }
                }
                else
                {
                    if (StringToParse == "td { vertical-align: top; padding-left: 4px; padding-right: 4px; }")
                    {
                        StringToParse = "td { vertical-align: top; padding-left: 4px; padding-right: 4px; white-space: pre; }";
                    }
                    LeftStreamWriter.WriteLine(StringToParse);
                    RightStreamWriter.WriteLine(StringToParse);
                }

            }
            LeftStreamWriter.Close();
            RightStreamWriter.Close();
            streamreader.Close();
        }

        /// <summary>
        /// Public method for ComparedFile object creating
        /// </summary>
        /// <param name="filename">The full name of html file, which is a result of file comporation</param>
        /// <returns>ComparedFile object</returns>
        public static ComparedFile Create(string filename)
        {
            try
            {
                EditSomeTextInHeader(filename);
                return new ComparedFile(filename);
            }
            catch (Exception)
            {
                throw new FileNotFoundException("Report about two compared files not found!");
            }
        }

        /// <summary>
        /// Editing some text in header of html Report file which is a result of compare script
        /// </summary>
        /// <param name="filename">The name of the html file</param>
        private static void EditSomeTextInHeader(string filename)
        {
            StreamWriter TempStreamWriter = new StreamWriter(@"temp.html");
            StreamReader streamreader = new StreamReader(filename);

            while (!streamreader.EndOfStream)
            {
                string StringFromFile = streamreader.ReadLine();
                TempStreamWriter.WriteLine(StringFromFile);
                if (StringFromFile == "<body>")
                {
                    for (int i = 0; i < 10; i++)
                    {
                        streamreader.ReadLine();
                    }
                }
            }
            streamreader.Close();
            TempStreamWriter.Close();
            File.Copy(@"temp.html", filename, true);
        }

        #region Properties

        /// <summary>
        /// The left part of html file, which is a result of files comporation
        /// </summary>
        public string leftFile { get; set; }

        /// <summary>
        /// The right part of html file, which is a result of files comporation
        /// </summary>
        public string rightFile { get; set; }

        #endregion
    }
}
